package com.lagou.client;

import com.lagou.model.User;
import com.lagou.model.UserMessage;

import java.io.IOException;
import java.util.List;
import java.util.Scanner;


/**
 * 编程实现客户端的主界面绘制和相应功能的实现
 */
public class ClientView{

    /**
     * 为了可以使用输入输出流，采用合成复用原则实现
     */
    private ClientInitClose cic;

    public ClientView(ClientInitClose cic){
        this.cic = cic;
    }
    /**
     * 自定义成员方法实现客户端主界面的绘制
     * @throws IOException
     */
    public void clientMainPage() throws IOException, ClassNotFoundException {
        while (true) {
            System.out.println("  \n\n\t\t   在线考试系统");
            System.out.println("-------------------------------------------");
            System.out.print("   [1] 学员系统");
            System.out.println("     [2] 管理员系统");
            System.out.println("   [0] 退出系统");
            System.out.println("-------------------------------------------");
            System.out.println("请选择要进行的业务编号：");
            //Scanner sc = new Scanner(System.in);
            int choose = ClientScanner.getScanner().nextInt();
            switch (choose){
                case 1:
                    System.out.println("正在进入学院系统。。。");
                    break;
                case 2:
                    clientManagerLogin();
                    break;
                case 0:
                    System.out.println("正在退出系统。。。");
                    return;
                default:
                    System.out.println("输入错误，请重新选择！");
            }
        }
    }
    /**
     * 自定义成员方法实现管理员登录的功能
     * @throws IOException
     */
    private void clientManagerLogin() throws IOException, ClassNotFoundException {
        //1.准备管理员登录的相关资料
        System.out.println("请输入管理员的账户信息：");
        String username = ClientScanner.getScanner().next();
        System.out.println("请输入管理员的密码信息：");
        String password = ClientScanner.getScanner().next();
        UserMessage tum = new UserMessage("managerCheck",new User(username,password));
        // 2.将UserMessage类型的对象通过对象输出流发送给服务器
        cic.getOos().writeObject(tum);
        System.out.println("客户端发送管理员账户信息成功！");
        tum = (UserMessage) cic.getOis().readObject();
        if ("success".equals(tum.getType())){
            System.out.println("登录成功，欢迎使用！");
        }else{
            System.out.println("用户名或密码错误！");
            return;
        }
        System.out.println("---------------------------------");
        ClientAdministratorPage();



    }

    private void studentchoose(int choose) throws IOException, ClassNotFoundException {

        switch (choose){
            case 1:
                System.out.println("请输入要添加的学生姓名信息：");
                String username = ClientScanner.getScanner().next();
                System.out.println("请输入要添加的学生密码信息：");
                String password = ClientScanner.getScanner().next();
                StudentIOTest("add",new User(username,password));
                break;
            case 2:
                System.out.println("请输入要修改的学生姓名信息：");
                String username1 = ClientScanner.getScanner().next();
                System.out.println("请输入要修改的学生密码信息：");
                String password1 = ClientScanner.getScanner().next();
                System.out.println("请输入修改后的学生姓名信息：");
                String username5 = ClientScanner.getScanner().next();
                System.out.println("请输入修改后的学生密码信息：");
                String password5 = ClientScanner.getScanner().next();
                StudentIOTest("re",new User(username1,password1));
                StudentIOTest("re1",new User(username5,password5));
                break;
            case 3:
                System.out.println("请输入要删除的学生姓名信息：");
                String username2 = ClientScanner.getScanner().next();
                System.out.println("请输入要删除的学生密码信息：");
                String password2 = ClientScanner.getScanner().next();
                StudentIOTest("del",new User(username2,password2));
                break;
            case 4:
                System.out.println("请输入要查询的学生姓名信息：");
                String username3 = ClientScanner.getScanner().next();
                System.out.println("请输入要查询的学生密码信息：");
                String password3 = ClientScanner.getScanner().next();
                StudentIOTest("at",new User(username3,password3));
                break;
            case 0:
                StudentIOTest("esc",new User("esc","esc"));
        }
    }
    private void StudentIOTest(String type,User user) throws IOException, ClassNotFoundException {
        UserMessage tum = new UserMessage(type,user);
        //将UserMessage类型的对象通过对象输出流发送给服务器
        cic.getOos().writeObject(tum);
        System.out.println("客户端发送操作信息成功！");
        tum = (UserMessage) cic.getOis().readObject();
        switch (tum.getType()) {
            case "addsuccess":
                System.out.println("添加学生成功");
                System.out.println("新学员姓名为"+tum.getUser().getUserName()+",密码为"+tum.getUser().getPassWord());
                break;
            case "resuccess":
                System.out.println("修改学生成功");break;
            case "delsuccess":
                System.out.println("删除学生成功");
                System.out.println("删除学员姓名为"+tum.getUser().getUserName()+",密码为"+tum.getUser().getPassWord());
                break;
            case "atsuccess":
                System.out.println("查询学生成功");

                System.out.println("查询学员为"+tum.getUser().getUserName()+",密码为"+tum.getUser().getPassWord());
                break;
            case "atfail":
                System.out.println("查询失败");
            default:
        }
        System.out.println("---------------------------------");
    }

    /**
     * 管理员系统界面
     */
    public void ClientAdministratorPage() throws IOException, ClassNotFoundException {
        while (true) {
            System.out.println("  \n\n\t\t   管理员系统");
            System.out.println("-------------------------------------------");
            System.out.print("   [1] 学员管理模块");
            System.out.println("     [2] 考题管理模块");
            System.out.println("   [0] 返回上一级");
            System.out.println("-------------------------------------------");
            System.out.println("请选择要进行的业务编号：");
            int choose = ClientScanner.getScanner().nextInt();
            switch (choose){
                case 1:
                    ClientAdministratorStudentPage();
                    break;
                case 2:
                    System.out.println("正在进入考题管理模块。。。");;
                    break;
                case 0:
                    System.out.println("正在返回上一级。。。");
                    return;
                default:
                    System.out.println("输入错误，请重新选择！");
            }
        }


    }

    /**
     * 学员管理模块界面
     */
    public void ClientAdministratorStudentPage() throws IOException, ClassNotFoundException {
        while (true) {
            System.out.println("  \n\n\t\t   学员管理模块");
            System.out.println("-------------------------------------------");
            System.out.print("   [1] 添加学员");
            System.out.println("     [2] 修改学员");
            System.out.print("   [3] 删除学员");
            System.out.println("     [4] 查询学员");
            System.out.println("   [0] 返回上一级");
            System.out.println("-------------------------------------------");
            System.out.println("请选择要进行的业务编号：");
            int choose = ClientScanner.getScanner().nextInt();
            switch (choose){
                case 1:
                    System.out.println("正在进入添加学员。。。");
                    studentchoose(choose);
                    break;
                case 2:
                    System.out.println("正在进入修改学员。。。");
                    studentchoose(choose);
                    break;
                case 3:
                    System.out.println("正在进入删除学员。。。");
                    studentchoose(choose);
                    break;
                case 4:
                    System.out.println("正在进入查询学员。。。");
                    studentchoose(choose);
                    break;
                case 0:
                    System.out.println("正在返回上一级。。。");
                    studentchoose(choose);
                    return;
                default:
                    System.out.println("输入错误，请重新选择！");
            }
        }
    }

}
